import {
  Store as VuexStore,
  CommitOptions,
  DispatchOptions,
  Module
} from 'vuex'
import state from './state'
import mutations, { UserMutations, UserMutationTypes } from './mutations'
import actions, { UserActions, UserActionTypes } from './actions'
import type { UserState } from './state'
import { RootState } from '@/store'

export { UserState, UserMutationTypes, UserActionTypes }

/**
 * TODO
 * Parameters<Mutations[K]>[1]
 * 取 Mutations函数的第二个参数的类型
 * 例如 (context, payload) => any。取payload的参数
 */
export type UserStore<U = UserState> = Omit<
  VuexStore<U>,
  'getters' | 'commit' | 'dispatch'
> & {
  commit<
    K extends keyof UserMutations,
    P extends Parameters<UserMutations[K]>[1]
  >(
    key: K,
    payload: P,
    options?: CommitOptions
  ): ReturnType<UserMutations[K]>
} & {
  dispatch<K extends keyof UserActions>(
    key: K,
    payload: Parameters<UserActions[K]>[1],
    options?: DispatchOptions
  ): ReturnType<UserActions[K]>
}

export const userStore: Module<UserState, RootState> = {
  state,
  mutations,
  actions
}
